Binarycrossentropy

计算二元交叉熵损失。用于衡量二分类问题中预测值 (input_x) 和目标值 (input_y) 之间的差距。

\[L_n = -w_n [y_n \cdot \log(x_n) + (1 - y_n) \cdot \log(1 - x_n)]\]

其中 \(x_n\) 是预测值, \(y_n\) 是目标值, \(w_n\) 是可选的样本权重。最终输出由 reduction 参数决定:

  • None (0): 不进行规约,输出每个元素的损失 \(L_n\)

  • Mean (1): 输出所有元素损失的平均值。

  • Sum (2): 输出所有元素损失的总和。

输入:
  • input_size - 输入张量的总元素数量。

  • reduction - 规约类型 (0: None, 1: Mean, 2: Sum)。

  • input_x - 预测值的张量数据地址,通常是Sigmoid函数的输出。

  • input_y - 目标值(标签)的张量数据地址。

  • weight - (可选) 权重张量的数据地址,维度与 input_x 相同。

  • loss - (输出) 最终损失值的数据地址。如果 reductionNone,其大小为 input_size;否则大小为1。

  • tmp_loss - 用于存储逐元素损失的临时工作空间地址,大小必须为 input_size

  • weight_defined - 权重是否有效的标志。若为非0,则`weight`参数必须提供。

  • core_mask - 核掩码。

输出:
  • loss - 写入最终损失值的数据地址。

支持平台:

FT78NE MT7004

备注

  • FT78NE 支持fp32, int8

  • MT7004 支持fp16, fp32

共享存储版本:

void fp_binarycrossentropy_s(int input_size, int reduction, float *input_x, float *input_y, float *weight, float *loss, float *tmp_loss, int weight_defined, int core_mask)
void hp_binarycrossentropy_s(int input_size, int reduction, half *input_x, half *input_y, half *weight, half *loss, half *tmp_loss, int weight_defined, int core_mask)
void i8_binarycrossentropy_s(int input_size, int reduction, int8_t *input_x, int8_t *input_y, int8_t *weight, int8_t *loss, int8_t *tmp_loss, int weight_defined, int core_mask)

C调用示例:

 1//FT78NE示例
 2#include <stdio.h>
 3#include <binarycrossentropy.h>
 4int main(int argc, char* argv[]) {
 5    float *input_x = (float *)0xA0000000;    // input_x 在DDR空间
 6    float *input_y = (float *)0xB0000000;    // input_y
 7    float *weight = (float *)0xC0000000;     // weight
 8    float *loss = (float *)0xD0000000;       // loss output
 9    float *tmp_loss = (float *)0xE0000000;   // temp workspace
10
11    int input_size = 1024;
12    int reduction = 1; // Mean
13    int weight_defined = 1; // true
14    int core_mask = 0xff;
15
16    fp_binarycrossentropy_s(input_size, reduction, input_x, input_y, weight, loss, tmp_loss, weight_defined, core_mask);
17    return 0;
18}

私有存储版本:

void fp_binarycrossentropy_p(int input_size, int reduction, float *input_x, float *input_y, float *weight, float *loss, float *tmp_loss, int weight_defined)
void hp_binarycrossentropy_p(int input_size, int reduction, half *input_x, half *input_y, half *weight, half *loss, half *tmp_loss, int weight_defined)
void i8_binarycrossentropy_p(int input_size, int reduction, int8_t *input_x, int8_t *input_y, int8_t *weight, int8_t *loss, int8_t *tmp_loss, int weight_defined)

C调用示例:

 1//FT78NE示例
 2#include <stdio.h>
 3#include <binarycrossentropy.h>
 4int main(int argc, char* argv[]) {
 5    float *input_x = (float *)0x10000000;    // input_x 在L2空间
 6    float *input_y = (float *)0x11000000;    // input_y
 7    float *weight = (float *)0x12000000;     // weight
 8    float *loss = (float *)0x13000000;       // loss output
 9    float *tmp_loss = (float *)0x14000000;   // temp workspace
10
11    int input_size = 1024;
12    int reduction = 1; // Mean
13    int weight_defined = 0; // false
14
15    fp_binarycrossentropy_p(input_size, reduction, input_x, input_y, weight, loss, tmp_loss, weight_defined);
16    return 0;
17}